package cate.game.role.res.guild.po;

import cate.common.table.d.GDFunc;
import cate.common.table.guild.GuildGradeRow;
import cate.game.role.Role;
import cate.game.role.res.guild.GuildFuncBase;
import cate.game.role.res.guild.msg.GuildActiveResp;
import cate.game.role.res.guild.msg.GuildDetailResp;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

@NoteClass(value = "公会活跃度")
public class GuildActivePO extends GuildFuncBase {

    @NoteField(value = "活跃度")
    public int activity;

    @NoteField(value = "当天活跃度累计")
    public int activityToday;

    @NoteField(value = "周活跃")
    public int activeWeek;

    @NoteField(value = "最后一次增加活跃度")
    public long lastTime;

    public synchronized void addActive(int activeValue) {
        this.activity += activeValue;
        GuildGradeRow row = funcDependent.guild.basic.getRow();
        if (row != null) {
            this.activity = Math.min(this.activity, row.activeMax);
        }
        this.activityToday += activeValue;
        this.activeWeek += activeValue;
        getFuncDependent().guild.basic.activeWeek = activeWeek;
        lastTime = System.currentTimeMillis();
        getFuncDependent().guild.noticeOnline(this::noticeOnline);
        noticeOnline();
    }

    private void noticeOnline(Role role) {
        role.sendNow(new GuildDetailResp(getFuncDependent().guild));
    }

    public synchronized boolean reduceActive(int activity) {
        if (this.activity >= activity) {
            this.activity -= activity;
            return true;
        }
        return false;
    }

    public void onDaySpan() {
        activityToday = 0;
    }

    public void onWeekSpan() {
        activeWeek = 0;
        getFuncDependent().guild.basic.activeWeek = 0;
    }

    @Override
    public int getFuncId() {
        return GDFunc.GuildFunc.TASK;
    }

    public void noticeOnline() {
        funcDependent.guild.noticeOnline(this::noticeUpdate);
    }

    public void noticeUpdate(Role role) {
        role.sendNow(new GuildActiveResp(this));
    }

}
